package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.dto.OrderSettingDTO;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.result.Result;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.util.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/ordersetting")
@Slf4j
public class OrderSettingController {
    @Autowired
    private AliOssUtil aliOssUtil;
    @Autowired
    private OrderSettingService orderSettingService;
    /**
     * 文件上传
     * @param excelFile
     * @return
     */
    @PostMapping("/upload")
    public Result upload(MultipartFile excelFile){
        log.info("文件上传：{}",excelFile);

        //原始文件名
        String originalFilename = excelFile.getOriginalFilename();
        String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
        String name = UUID.randomUUID().toString() + extension;

        try {
            String filePath = aliOssUtil.upload(excelFile.getBytes(), name);
            return new Result(true, MessageConst.UPLOAD_SUCCESS,filePath);
        } catch (IOException e) {
            log.error("文件上传失败");
        }

        return new Result(false,MessageConst.IMPORT_ORDERSETTING_FAIL);
    }
    /**
     * 查看预约情况
     * @param year
     * @param month
     */
    @GetMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String year,String month){

        List<OrderSettingDTO> orderSettingList = orderSettingService.getOrderSettingByMonth(year, month);
        return new Result(true,MessageConst.GET_ORDERSETTING_SUCCESS,orderSettingList);
    }

    /**
     * 设置预约人数
     * @param orderSetting
     * @return
     */
    @PostMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting){
        orderSettingService.editNumberByDate(orderSetting);
        return new Result(true,MessageConst.ORDERSETTING_SUCCESS);
    }
}
